package app.server.project.main.service.db.entity.user;

import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.syndb.SynSpeed;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_ip")
@TableName("user_ip")
@Comment(value = "用户ip追踪表表")
@SynSpeed
public class UserIPEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("user_id")
    @Comment("用户id")
    @Column(name = "user_id")
    private long userId;

    @TableField("login_time")
    @Comment("登录时间")
    @Column(name = "login_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime loginTime;

    @TableField("ip")
    @Comment("ip")
    @Column(name = "ip")
    private String ip;

    @TableField("country")
    @Comment("国家")
    @Column(name = "country")
    private String country;

    @TableField("lbs")
    @Comment("定位详情")
    @Column(name = "lbs", length = Integer.MAX_VALUE)
    private String lbs;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static UserIPEntity of(long userId, long id) {
        var ret = new UserIPEntity();
        ret.setId(id);
        ret.setSynDb(true);
        ret.setUserId(userId);
        return ret;
    }

    public void setLoginTime(LocalDateTime loginTime) {
        this.loginTime = loginTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserIPEntity::getLoginTime, this);
    }

    public void setIp(String ip) {
        this.ip = ip;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserIPEntity::getIp, this);
    }

    public void setCountry(String country) {
        this.country = country;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserIPEntity::getCountry, this);
    }

    public void setLbs(String lbs) {
        this.lbs = lbs;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserIPEntity::getLbs, this);
    }


    public void setUserId(long userId) {
        this.userId = userId;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserIPEntity::getUserId, this);
    }

}
